/**
 * 
 */
package edu.umd.clip.lm.util;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class ConstCountDistribution {
	private final CompactReadOnlyLong2LongHashMap counts;
	private final long totalCount;
	
	public ConstCountDistribution(Long2IntMap counts) {
		long keys[] = new long[counts.size()];
		long values[] = new long[counts.size()];
		
		long totalCount = 0;
		int pos = 0;
		for(Long2IntMap.Iterator it = counts.iterator(); it.hasNext(); ) {
			Long2IntMap.Entry e = it.next();
			keys[pos] = e.getKey();
			values[pos] = e.getValue();
			totalCount += e.getValue();
			++pos;
		}
		
		this.counts = new CompactReadOnlyLong2LongHashMap(keys, values, 2.0f);
		this.totalCount = totalCount;
	}
	
	public ConstCountDistribution(Long2LongMap counts) {
		long keys[] = new long[counts.size()];
		long values[] = new long[counts.size()];
		
		long totalCount = 0;
		int pos = 0;
		for(Long2LongMap.Iterator it = counts.iterator(); it.hasNext(); ) {
			Long2LongMap.Entry e = it.next();
			keys[pos] = e.getKey();
			values[pos] = e.getValue();
			totalCount += e.getValue();
			++pos;
		}
		
		this.counts = new CompactReadOnlyLong2LongHashMap(keys, values, 2.0f);
		this.totalCount = totalCount;
	}
	
	public ConstCountDistribution(long keys[], long values[]) {
		this.counts = new CompactReadOnlyLong2LongHashMap(keys, values, 2.0f);
		long totalCount = 0;
		for(long v : values) {
			totalCount += v;
		}
		this.totalCount = totalCount;
	}
	
	public Long2IntMap toLong2IntMap() {
		Long2IntMap result = new Long2IntMap(counts.size());
		long keys[] = counts.keys();
		long values[] = counts.values();
		for(int i=0; i<keys.length; ++i) {
			result.put(keys[i], (int)values[i]);
		}
		return result;
	}

	public Long2LongMap toLong2LongMap() {
		Long2LongMap result = new Long2LongMap(counts.size());
		long keys[] = counts.keys();
		long values[] = counts.values();
		for(int i=0; i<keys.length; ++i) {
			result.put(keys[i], values[i]);
		}
		return result;
	}

	public long getTotalCount() {
		return totalCount;
	}

	public final int size() {
		return counts.size();
	}

	public long[] keys() {
		return counts.keys();
	}

	public long[] values() {
		return counts.values();
	}

	public final long get(long key) {
		return counts.get(key);
	}
}
